package com.v1.video.activity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Window;
import android.widget.Toast;

import com.iss.app.IssActivity;
import com.umeng.analytics.MobclickAgent;
import com.v1.video.AppContext;
import com.v1.video.AppManager;
import com.v1.video.Constant;
import com.v1.video.service.StatisticsInEndService;
import com.v1.video.util.Logger;

public abstract class BaseActivity extends IssActivity {
	@Override
	protected void initView() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub

	}

	public static final int PAGE_SIZE = Constant.PAGE_SIZE;
	public static final int TEXT_LENGTH = 140;
	protected static String TAG;

	private AppContext app;
	private Thread mThread = null;
	private static StatisticsInEndService iService = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		TAG = this.getClass().getSimpleName();

		AppManager.getAppManager().addActivity(this);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//横屏

		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);// 竖屏
		super.onCreate(savedInstanceState);
		app = AppContext.getInstance();
		bindService();
		app.setActivityState(this, true);
	}

	private void bindService() {
		Intent intent = new Intent(this, StatisticsInEndService.class);
		bindService(intent, connection, Context.BIND_AUTO_CREATE);
	}

	private static final ServiceConnection connection = new ServiceConnection() {
		public void onServiceDisconnected(ComponentName name) {
			Logger.d(TAG, "Activity ->Disconnected the LocalService");
		}

		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			// 获取连接的服务对象
			iService = (StatisticsInEndService) service;
			Logger.d(TAG, "Activity ->Connected the Service");
		}
	};

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (mThread != null) {
			mThread.interrupt();
		}
		app.setActivityState(this, true);
	}

	@Override
	protected void onStop() {
		super.onStop();
		mThread = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(500);
					app.setActivityState(BaseActivity.this, false);
					app.removeActivityState(BaseActivity.this);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		});
		mThread.start();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unbindService(connection);
		AppManager.getAppManager().finishActivity(this);
	}

	// @Override
	// protected void onResume() {
	// super.onResume();
	// MobclickAgent.onResume(this);
	// }
	// @Override
	// protected void onPause() {
	// super.onPause();
	// MobclickAgent.onPause(this);
	// }

	protected void toast(CharSequence text) {
		Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
	}

	/**
	 * 显示Toast信息
	 * 
	 * @param message
	 */
	protected void showToast(String message) {
		Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
	}

	/**
	 * 显示Toast信息
	 * 
	 * @param resId
	 */
	protected void showToast(int resId) {
		Toast.makeText(this, resId, Toast.LENGTH_SHORT).show();
	}

	/**
	 * 跳转页面关闭当前页面
	 * 
	 * @param intent
	 */
	public void openActivity(Intent intent) {
		startActivity(intent);
		this.finish();
	}

	/**
	 * 统计
	 * 
	 * @param tag
	 */
	public void clickStatistics(String tag) {
		MobclickAgent.onEvent(this, tag);
	}

	/**
	 * 统计
	 * 
	 * @param tag
	 * @param text
	 */
	public void clickStatistics(String tag, String text) {
		MobclickAgent.onEvent(this, tag, text);
	}
	
    /**
     * 返回当前activity是否为横屏
     *
     * @return
     */
    public boolean isLandScape() {
        return getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
    }
    
    void toLand() {
        if (Build.VERSION.SDK_INT >= 9) {
            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
        } else {
            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
    }

    public void toPortrait() {
        if (isLandScape()) {
            if (Build.VERSION.SDK_INT >= 9) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
            } else {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }
        }
    }
}
